package com.computergames.server.service.Implement;

import com.computergames.server.message.AbstractGameMessage;
import com.computergames.server.message.cardMessage.AbstractCardMessage;
import com.computergames.server.message.chessMessage.AbstractChessMessage;
import com.computergames.server.server.session.RoomSessionFactory;
import com.computergames.server.server.session.RoomSessionService;
import com.computergames.server.service.Interface.GameStorage;
import com.computergames.server.util.RedisUtil;
import com.computergames.server.util.SpringUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LandlordStorage implements GameStorage {
    private final static LandlordStorage INSTANCE = new LandlordStorage();

    private LandlordStorage()
    {

    }

    public static LandlordStorage getInstance()
    {
        return INSTANCE;
    }

    private final Map<String, List<String>> landlordStorageMap = new ConcurrentHashMap<>();

    @Override
    public void save(String roomName, AbstractGameMessage content, int result, ChannelHandlerContext ctx) {
        AbstractCardMessage cardContent = (AbstractCardMessage)content;
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        String[] cards = cardContent.getCard();
        String player = (String)ctx.attr(AttributeKey.valueOf("POSITION")).get();
        RoomSessionService roomSessionService = RoomSessionFactory.getRoomSession();
        List<Channel> channels = roomSessionService.getMembersChannel(roomName);

        if(cards != null){
            landlordStorageMap.computeIfAbsent(roomName, k -> new ArrayList<>());

            StringBuffer sb = new StringBuffer();
            if ("A".equals(player)) {
                sb.append("A(");
            } else if ("B".equals(player)) {
                sb.append("B(");
            } else if ("C".equals(player)) {
                sb.append("C(");
            }

            for (int i = 0; i < cards.length; i++) {
                sb.append(cards[i]);
                if (i < cards.length - 1) {
                    sb.append(",");
                }
            }
            sb.append(")");

            String record = sb.toString();

            landlordStorageMap.get(roomName).add(record);

            if(result == 1){
                //保存牌谱
                Map<String, Object> recordsData = new ConcurrentHashMap<>();
                List<String> records = landlordStorageMap.get(roomName);
                recordsData.put(roomName, records);
                redisUtil.hmset(roomName + "牌谱", recordsData);
                //保存积分
                String stage = (String)ctx.channel().attr(AttributeKey.valueOf("USER_STAGE")).get();
                for(Channel channel : channels){
                    String username = (String)channel.attr(AttributeKey.valueOf("USER_IDENTIFIER")).get();
                    if(channel.equals(ctx.channel())){
                        redisUtil.hincr(roomName + stage + "积分", username, 2);
                    }else{
                        redisUtil.hincr(roomName + stage + "积分", username, 0);
                    }
                }
                landlordStorageMap.remove(roomName);
                recordsData.remove(roomName);
            }
            }
    }

    @Override
    public List<String> getValuesForKey(String key) {
        return landlordStorageMap.get(key);
    }
}

